﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Wicresoft.SelfService.WFInterface;
using System.Threading;

namespace Wicresoft.SelfService.WorkFlow
{

    public sealed class CreateBookMark : NativeActivity<ActionParam>
    {
        public InArgument<string> Step { get; set; }
        public InArgument<ActionResult> actionResult { get; set; }

        protected override void Execute(NativeActivityContext context)
        {
            string name = "waitingFor_" + this.Step.Get(context).ToString();

            if (Step.Get(context) == null)
            {
                throw new Exception("Application is required");
            }

            context.CreateBookmark(name, new BookmarkCallback(OnReadComplete));

            if (actionResult != null)
            {

                WorkflowCache.WFActionResults[context.WorkflowInstanceId] = this.actionResult.Get(context);
            }
        }

        void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
        {
            context.SetValue(this.Result, (ActionParam)state);
        }

        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }
    }
}
